<?php

class noLogin extends bhCommon {

    public function __construct()
    {
        parent::__construct();
    }



    /**
     * 登录
     */
    public function login()
    {
        $code = $this->get('code');
        if (empty($code)) {
            $this->json('参数错误', 0);
        }

        $option = array(
            'appid' => $this->w['uniaccount']['key'],
            'secret' => $this->w['uniaccount']['secret'],
        );

        if (isQq()) {
            $wxObj = new Qqadoc($option);
        } else {
            $wxObj = new Wxadoc($option);
        }

        if (false === $res = $wxObj->jscode2session($code)) {
            $this->json($wxObj->getError(), 0);
        }
        $member = p_get('member', array('openid' => $res['openid']));

        if (empty($member)) {
            $data = array(
                'openid' => $res['openid'],
                'created' => time()
            );
            if ($res['unionid']) {
                $data['unionid'] = $res['unionid'];
                $member = p_get('member', array('unionid' => $res['unionid']));
            }
        }

        if (empty($member)) {
            if ($this->get('parent_id')) {
                $data['parent_id'] = $this->get('parent_id');
            }
            // 注册赠与积分
            $integral = p_getcolumn("config", array('key' => 'get_group'), 'value');
            $data['integral'] = empty($integral) ? 15 : $integral;
            p_insert('member', $data);
            $member['id'] = pdo_insertid();

            if (isset($data['parent_id']) && $data['parent_id'] > 0) {
                //$this->rising->update_group($data['parent_id'], $member['id']);
            }
        }

        $token = md5($member['id'] . time() . PREFIX);

        $insert = array(
            'token' => $token,
            'session_key' => $res['session_key'],
            'member_id' => $member['id'],
            'created' => time()
        );

        p_insert('token', $insert);

        $this->json($token, 1, false);
    }


    /**
     * 图片上传
     */
    public function upload()
    {
        if (!isset($_FILES['file'])) {
            json('请上传图片', 0);
        }
        load()->func('file');
        $type = $this->get('type');
        $reslut = file_upload($_FILES['file'], $type);
        if (isset($reslut['errno'])) {
            json($reslut['message'], 0);
        }
        $pic = $reslut['path'];

        if (is_oss()) {
            $remotestatus = file_remote_upload($reslut['path']);
            if (is_error($remotestatus)) {
                json('远程附件上传失败', 0);
            }
        }

        json(array('url' => tomedia($pic), 'path' => $pic));
    }


    public function decode()
    {
        $path = $this->get('path');
        $path = urldecode($path);
        $this->json(array('path' => $path));
    }


    /**
     * 审核模式
     */
    public function examine()
    {
        $audit_model = getConfig('audit_model');
        $this->json(array('audit_model' => $audit_model));
    }


    public function loading()
    {
        $full_screen_ad = getConfig('full_screen_ad');
        $ad_time = getConfig('ad_time');

        $data = array(
            'full_screen_ad' => $full_screen_ad,
            'ad_time' => $ad_time,
        );
        $this->json($data);
    }
}